import { isFunction, isUndefined } from "@tarojs/shared";
import Taro from "@tarojs/taro";
import { shBaseUrl } from "./urls";
import useUserInfoStore from "../store/user"
import dayjs from "dayjs";
const state = useUserInfoStore()
// 请求拦截器
const interceptor = function (chain) {

  var requestParams = chain.requestParams;
  var p;
  var res: any = new Promise(function (resolve, reject) {
    p = chain.proceed(requestParams)
    p.then(function (res) {
      if (res.statusCode === 200) {
        resolve(res);
      } else {
        Taro.showToast({
          icon: 'error',
          title: res.errMsg
        })
        reject(Error('错误'))
      }
    }).catch(function (err) {
      reject(err);
    });
  });
  if (!isUndefined(p) && isFunction(p.abort)) res.abort = p.abort;
  return res;
}

Taro.addInterceptor(Taro.interceptors.timeoutInterceptor)
Taro.addInterceptor(interceptor)


export async function $post<TResp extends string | Record<string, any> | ArrayBuffer, TData = any>(urlKey: string, data?: TData): Promise<Taro.request.SuccessCallbackResult<TResp>> {
  let url
  if (urlKey.includes('http://')) {
    url = urlKey
  } else {
    url = shBaseUrl + urlKey
  }
  return (await Taro.request({
    method: 'POST',
    header: { 'content-type': 'application/json', 'Authorization': state.token, },
    url: url,
    data: data
  }))
}
export async function $get<TResp extends string | Record<string, any> | ArrayBuffer, TData = any>(urlKey: string, data: TData): Promise<Taro.request.SuccessCallbackResult<TResp>> {
  let url
  if (urlKey.includes('http://')) {
    url = urlKey
  } else {
    url = shBaseUrl + urlKey
  }
  return (await Taro.request({
    method: 'GET',
    header: { 'content-type': 'application/json', 'Authorization': state.token, },
    url: url,
    data: data
  }))
}
export async function $del<TResp extends string | Record<string, any> | ArrayBuffer, TData = any>(urlKey: string, data: TData): Promise<Taro.request.SuccessCallbackResult<TResp>> {
  let url
  if (urlKey.includes('http://')) {
    url = urlKey
  } else {
    url = shBaseUrl + urlKey
  }
  return (await Taro.request({
    method: 'DELETE',
    header: { 'content-type': 'application/json', 'Authorization': state.token, },
    url: url,
    data: data
  }))
}

interface OrderListParams{
    /**
     *  用customer查询 用户的全部单子
     */
    customerCode:string,
    wxStatus: string|number,
    startBillingTime:string,
    endBillingTime:string,
    flag:string,
    keywords:string,
    //ispopuppickup:boolean,
    shipperMobile?:string,
    receiverMobile?:string,
  }


  export function orderList(data: OrderListParams,pageNum:number,pageSize:number): Promise<any> {
    return new Promise<any>(async (resolve, reject) => {
      const res = await $post<{ code: number, rows: any, msg: string,total:number }>(`/selfmachine-system/logistics/informationList?pageNum=${pageNum}&pageSize=${pageSize}`, data)
      if (res.data.code == 200) {

        resolve(res)
      } else {
        Taro.showToast({
          icon: 'error',
          title: res.data.msg
        })
        reject(res.data.msg)
      }
    })
  }

export function getOrderTrackingList(data: OrderListParams,pageNum:number,pageSize:number): Promise<any> {
  return new Promise<any>(async (resolve, reject) => {
    const res = await $post<{ code: number, rows: any, msg: string,total:number }>(`/selfmachine-system/logistics/cargoTracing?pageNum=${pageNum}&pageSize=${pageSize}`, data)
    if (res.data.code == 200) {

      resolve(res)
    } else {
      Taro.showToast({
        icon: 'error',
        title: res.data.msg
      })
      reject(res.data.msg)
    }
  })
}



  export function getLogisticsInfo(data:string): Promise<any> {
    return new Promise<any>(async (resolve, reject) => {
      const res = await $post<{ code: number, data: any, msg: string }>(`/selfmachine-system/logistics/getLogisticsInfo/${data}`)
      if (res.data.code == 200) {
        console.log(res.data.data,'物流轨迹');

        resolve(res.data.data)
      } else {
        Taro.showToast({
          icon: 'error',
          title: res.data.msg
        })
        reject(res.data.msg)
      }
    })
  }
